package 力扣日常刷题.木22二月.第01天206;

import 力扣日常刷题.TreeNode;

import java.util.*;

/**
 * @author 帅小伙
 * @date 2022/2/6
 * @description
 */
public class Demo09JZ打印二叉树 {


    public int[] levelOrder(TreeNode root) {
        if(root==null){
            return new int[0];
        }
        List<Integer> ans = new ArrayList<>();
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while(!queue.isEmpty()) {
            for (int i = queue.size(); i > 0; i--) {
                TreeNode poll = queue.poll();
                ans.add(poll.val);
                if(poll.left!=null) queue.add(poll.left);
                if(poll.right!=null) queue.add(poll.right);
            }
        }
        int[] res = new int[ans.size()];
        for (int i = 0; i < ans.size(); i++) res[i] = ans.get(i);
        return res;
    }


}
